package com.example.mythealthapp

import android.app.*
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Binder
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
import com.example.mythealthapp.guide.MainActivity

class MyService : Service(){

    private val mBinder=Binder()
    override fun onBind(intent: Intent?): IBinder? {
        return mBinder
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onCreate() {
        super.onCreate()
        Log.d("MyService","onCreate")
        val ID = "com.example.mythealthapp"
        val NAME = "前台Service通知"
        val manager=getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            val channel=NotificationChannel(ID,NAME,NotificationManager.IMPORTANCE_DEFAULT)
            channel.enableLights(true)
            channel.setShowBadge(true)
            channel.lockscreenVisibility=Notification.VISIBILITY_PUBLIC
            manager.createNotificationChannel(channel)
        }
        val intent=Intent(this,MainActivity::class.java)
        val pi=PendingIntent.getActivity(this,0,intent,0)
        val notification=NotificationCompat.Builder(this,ID)
            .setContentTitle("健康膳食运行中")
            .setContentText("快进来看看吧")
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setLargeIcon(BitmapFactory.decodeResource(resources,R.mipmap.honey))
            .setContentIntent(pi)
            .build()
        startForeground(1,notification)
    }

    override fun onDestroy() {
        super.onDestroy()
    }


}